//
//  ZLGlobalColorConfig.m
//  zhilian
//
//  Created by mac on 2020/11/24.
//  Copyright © 2020 LZT. All rights reserved.
//

#import "UIColor+Additions.h"

@implementation UIColor (Additions)

const float FLOAT_1_255 = 1.f / 255;

# define RGB2FLOAT(val) ((val) * FLOAT_1_255)
# define FLOAT2RGB(val) ((val) * 255)

# define ARGB_ALPHA(val) (((val) & 0xff000000) >> 24)
# define RGB_RED(val) (((val) & 0xff0000) >> 16)
# define RGB_GREEN(val) (((val) & 0xff00) >> 8)
# define RGB_BLUE(val) ((val) & 0xff)

+ (UIColor*)mainRed{
    return  [UIColor colorWithHexAString:@"#E51C23"];
}

+ (UIColor*)mainBlue{
    return [UIColor colorWithHexAString:@"#3366CC"];
}

+ (UIColor*)mainBlack{
    return [UIColor colorWithHexAString:@"#333333"];
}

+ (UIColor*)mainWhite{
    return [UIColor colorWithHexAString:@"#FFFFFF"];
}

/// 辅助性使用，着重信息
+(UIColor *)assistOrange{
    return [UIColor colorWithHexAString:@"#F6A80B"];
}

+(UIColor *)assistYellow{
    return [UIColor colorWithHexAString:@"#F5DB36"];
}

+(UIColor *)assistRoseRed{
    return [UIColor colorWithHexAString:@"#F34279"];
}

/// 用于不可点击的状态
+(UIColor *)assistPink{
    return [UIColor colorWithHexAString:@"#E51C23"];
}

/// 用于不可点击的状态（商户中心）
+(UIColor *)assistSkyBlue{
    return [UIColor colorWithHexAString:@"#A0B5EF"];
}

/// 用于次要展示内容、icon、按钮 描边 用于说明、辅助文字信息
+(UIColor *)assistGray{
    return [UIColor colorWithHexAString:@"#999999"];
}

/// 用于分组信息、段落之间的分割 线、置灰后的按钮
+(UIColor *)assistLightGray1{
    return [UIColor colorWithHexAString:@"#E7E7E7"];
}

/// 用于整体页面背景、内容输入框 背景、标签背景
+(UIColor *)assistLightWhite{
    return [UIColor colorWithHexAString:@"#F7F7F7"];

}

/// 用于个别标签背景 如购买数量标签背景
+(UIColor *)assistLightGray2{
    return [UIColor colorWithHexAString:@"#f2f2f2"];
}

#pragma mark 文字颜色
/// 用于次要文字信息
+(UIColor *)fontColorGray1{
    return [UIColor colorWithHexAString:@"#666666"];
}

/// 用于输入提示文字、不可点击内容
+(UIColor *)fontColorGray2{
    return [UIColor colorWithHexAString:@"#cccccc"];
}

+ (UIColor*)colorWithRGB:(int)rgb {
    return [UIColor colorWithRed:RGB2FLOAT(RGB_RED(rgb)) green:RGB2FLOAT(RGB_GREEN(rgb)) blue:RGB2FLOAT(RGB_BLUE(rgb)) alpha:1];
}

+ (UIColor*)colorWithRedi:(Byte)red green:(Byte)green blue:(Byte)blue alpha:(Byte)alpha {
    return [UIColor colorWithRed:RGB2FLOAT(red) green:RGB2FLOAT(green) blue:RGB2FLOAT(blue) alpha:RGB2FLOAT(alpha)];
}

+ (UIColor*)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue {
    return [UIColor colorWithRed:red green:green blue:blue alpha:1];
}

+ (UIColor*)colorWithRedi:(Byte)red green:(Byte)green blue:(Byte)blue {
    return [self colorWithRedi:red green:green blue:blue alpha:255];
}

+ (UIColor *)colorWithHexAString:(NSString *)hexColor{
    unsigned int alpha, red, green, blue;
    NSRange range;
    range.length =2;
    if (hexColor.length == 9) {//8位十六进制
        range.location =1;
        [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&alpha];//透明度
        range.location =3;
        [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&red];
        range.location =5;
        [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&green];
        range.location =7;
        [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&blue];
        return [UIColor colorWithRed:(float)(red/255.0f)green:(float)(green/255.0f)blue:(float)(blue/255.0f)alpha:(float)(alpha/255.0f)];
    } else {
        range.location =1;
        [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&red];
        range.location =3;
        [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&green];
        range.location =5;
        [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&blue];
        return [UIColor colorWithRed:(float)(red/255.0f)green:(float)(green/255.0f)blue:(float)(blue/255.0f)];
    }
}


+ (UIColor *)gradientColors:(NSArray*)colors
               gradientType:(GradientType)gradientType
                    imgSize:(CGSize)imgSize{
    NSMutableArray *ar = [NSMutableArray array];
    
    for(UIColor *c in colors) {
        [ar addObject:(id)c.CGColor];
    }
    
    UIGraphicsBeginImageContextWithOptions(imgSize, YES, 1);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);
    CGPoint start;
    CGPoint end;
    
    switch (gradientType) {
        case GradientTypeTopToBottom:
            start = CGPointMake(0.0, 0.0);
            end = CGPointMake(0.0, imgSize.height);
            break;
        case GradientTypeLeftToRight:
            start = CGPointMake(0.0, 0.0);
            end = CGPointMake(imgSize.width, 0.0);
            break;
        case GradientTypeUpleftToLowright:
            start = CGPointMake(0.0, 0.0);
            end = CGPointMake(imgSize.width, imgSize.height);
            break;
        case GradientTypeUprightToLowleft:
            start = CGPointMake(imgSize.width, 0.0);
            end = CGPointMake(0.0, imgSize.height);
            break;
        default:
            break;
    }
    
    CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGContextRestoreGState(context);
    CGColorSpaceRelease(colorSpace);
    UIGraphicsEndImageContext();
    
    return [UIColor colorWithPatternImage:image];

}

+ (UIColor *)randomColor {
    return [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];
};

@end
